var foodinfoManager;
var foodinfoItemPageOpen;
var foodinfoItem;

function initFoodInfoManager(){
	var colimnsArray = [];
		colimnsArray.push({
			display : '编号',
			name : 'id',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center"
		});
		colimnsArray.push({
			display : '餐品名',
			name : 'name',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			editor: { type: 'text'},
			isSort : false
		});
		colimnsArray.push({
			display : '餐品类型',
			name : 'type',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			render:function(item){
				for(var i in foodTypes){
					if(item.type == foodTypes[i].id){
						return foodTypes[i].name;
					}
				}
			}
		});
		colimnsArray.push({
			display : '社区',
			name : 'areaId',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			render:function(item){
				if(item.areaInfos){
					var html = '';
					for(var i in item.areaInfos){
						html += item.areaInfos[i].name+',';
					}
					return html;
				}
			}
		});
		colimnsArray.push({
			display : '状态',
			name : 'state',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			editor: { type: 'select',data:foodStateRenderData,valueColumnName:'value'},
			render:function(item){
				return defaultRenderFun(item.state,foodStateRenderData);
			}
		});
		colimnsArray.push({
			display : '商户',
			name : 'shopId',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			render:function(item){
				if(item.shopInfo){
					return item.shopInfo.name;
				}
			}
		});
		colimnsArray.push({
			display : '图片',
			name : 'pic',
			align : 'left',
			showTitle : false,
			minWidth : 50,
			align : "center",
			render :function(item){
				if(item.pic){
					return '<a target="_blank" href="'+item.pic+'">'+item.pic+'</a>';
				}
			}
		});
		colimnsArray.push({
			display : '标签价',
			name : 'labelPrice',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			editor: { type: 'text'},
			isSort : false
		});
		colimnsArray.push({
			display : '折扣价',
			name : 'realPrice',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			editor: { type: 'text'},
			isSort : false
		});
		colimnsArray.push({
			display : '供应量',
			name : 'maxNum',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			editor: { type: 'text'},
			isSort : false
		});
		colimnsArray.push({
			display : '营养价值',
			name : 'efficacy',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			editor: { type: 'text'},
			isSort : false
		});
		colimnsArray.push({
			display : '介绍',
			name : 'description',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			editor: { type: 'text'},
			isSort : false
		});
	
	var menu = $.ligerMenu({ width: 120, items:
		[
			{ text: '增加', click: function(){openFoodInfoItemPage('create',"新增")}, icon: 'add' },
			{ line: true },
			{ text: '修改信息', click: function(){openFoodInfoItemPage('edit',"编辑信息")} },
			{ line: true },
			{ text: '修改社区', click: function(){openRoodsInfoGrantPage()} },
			{ line: true },
			{ text: '修改图片', click: function(){toUploadImgPage()} },
			{ line: true },
			{ text: '查看', click: function(){openFoodInfoItemPage('show',"查看")} }
		]
	}); 
	
	return window['g'] = jQuery("#maingrid").ligerGrid({
		height : '100%',
		url:'/admin/food/foodInfo/ajaxList.do',
		columns : colimnsArray,
		pageSize : 10,
		rownumbers : true,
		toolbar : {
			items : [ {
				text : '保存',
				click : saveUpdateFoodInfoManager,
				icon : 'modify'
			}, {
				line : true
			}, {
				text : '增加',
				click : function(){openFoodInfoItemPage('create',"新增")},
				icon : 'add'
			}, {
				line : true
			}]
		},
		onContextmenu : function (parm,e)
        {
			foodinfoItem = parm.data;
	        menu.show({ top: e.pageY, left: e.pageX });
	        return false;
        },
        enabledEdit: false, 
        isScroll: false, 
        rownumbers:true
	});
};

function toUploadImgPage(){
	foodinfoItemPageOpen = $.ligerDialog.open({ url:'/admin/food/upload/img.do?foodId='+foodinfoItem.id,isResize:true,title:'修改图片-'+foodinfoItem.name,width:450 });
}

var foodsInfoGrantManger;
function doSaveFoodAreaInfos(newData,foodIdVal){
	jQuery.ajax({
		url:'/admin/food/foodAreaInfo/saveOrUpdate.do',
		data:{json:JSON.stringify(newData),foodId:foodIdVal},
		dataType:'json',
		type:'post',
		success:function(data){
			if(data.code == '0'){
				$.ligerDialog.success('恭喜,已经保存成功!');
				tip = $.ligerDialog.tip({ title: '提示信息', content: '恭喜,已经保存'+data.num+'条记录!' });
				foodinfoItemPageOpen.hidden();
			}else{
				if(data.msg){
					tip = $.ligerDialog.tip({ title: '提示信息', content: data.msg });
				}else{
					tip = $.ligerDialog.tip({ title: '提示信息', content: '网络异常,没有保存成功!' });
				}
			}
		},
		error:function(){
			$.ligerDialog.error("网络发生异常,请稍后再试!");
		}
	});
};
function openRoodsInfoGrantPage(){
	jQuery.ajax({
		url:'/admin/food/foodInfo/getAreas.do',
		data:{id:(foodinfoItem?foodinfoItem.id:0)},
		dataType:'json',
		type:'post',
		success:function(data){
			if(data){
				for(var i in data.allAreaInfo){
					for(var j in data.usedAreaInfo){
						if(data.allAreaInfo[i].id == data.usedAreaInfo[j].id){
							data.allAreaInfo[i].ischecked = true;
						}
					}
				}
				if(foodsInfoGrantManger){
					foodsInfoGrantManger.clear();
				}
				foodsInfoGrantManger = jQuery('#grantDiv').ligerTree({
					data:data.allAreaInfo,
					idFieldName :'id',
					textFieldName : 'name',
					parentIDFieldName :'pid'
				});
				foodinfoItemPageOpen = $.ligerDialog.open({ target: $("#grantDivWrapper"),isResize:true,title:'设置社区-'+data.foodInfo.name,width:450 });
				jQuery('#grantFoodInfoSave').unbind('click').click(function(){
					var notes = foodsInfoGrantManger.getChecked();
					var checkList = [];
					for (var i in notes){
						var temp = {};
						temp.foodId = data.foodInfo.id;
						temp.areaId = notes[i].data.id;
						checkList.push(temp);
					}
					jQuery.ligerDialog.confirm('确定要进行设置吗?',function (yes,value) {
						if(yes){
							doSaveFoodAreaInfos(checkList,data.foodInfo.id);
						}
						});
				});
				jQuery('#grantFoodInfoReset').unbind('click').click(function(){
					
				});
			}else{
				$.ligerDialog.error("无法读取数据,请稍后再试!");
			}
		},
		error:function(){
			$.ligerDialog.error("网络发生异常,请稍后再试!");
		}
	});
}

function saveUpdateFoodInfoManager(item){
	var data = foodinfoManager.getUpdated();
	if(data.length == 0){
		$.ligerDialog.warn("您还未做任何修改哦!");
		return;
	}
	doSaveFoodInfos(data);
};
function doSaveFoodInfos(newData,add){
	jQuery.ajax({
		url:'/admin/food/foodInfo/saveOrUpdate.do',
		data:{json:JSON.stringify(newData)},
		dataType:'json',
		type:'post',
		success:function(data){
			if(data.sucessList.length > 0){
				$.ligerDialog.success('恭喜,已经保存成功!');
				tip = $.ligerDialog.tip({ title: '提示信息', content: '恭喜,已经保存'+data.sucessList.length+'条记录!' });
				foodinfoManager.loadServerData({pagesize:10});
				foodinfoItemPageOpen.hidden();
			}else{
				if(data.msg){
					tip = $.ligerDialog.tip({ title: '提示信息', content: data.msg });
				}else{
					tip = $.ligerDialog.tip({ title: '提示信息', content: '网络异常,没有保存成功!' });
				}
			}
		},
		error:function(){
			$.ligerDialog.error("网络发生异常,请稍后再试!");
		}
	});
};
/**
 * 
 * @param item
 * @param target
 */
function openFoodInfoItemPage(target,titleName){
	jQuery.ajax({
		url:'/admin/food/foodInfo/'+target+'.do',
		data:{id:(foodinfoItem?foodinfoItem.id:0)},
		dataType:'text',
		type:'post',
		success:function(data){
			if(data){
				jQuery("#openDiv").html(data);
				foodinfoItemPageOpen = $.ligerDialog.open({ target: $("#openDiv"),isResize:true,title:titleName,width:450 });
			}
		},
		error:function(){
			$.ligerDialog.error("网络发生异常,请稍后再试!");
		}
	});
};

jQuery(function() {
	foodinfoManager = initFoodInfoManager();
    jQuery("#pageloading").hide();
});